<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      Object.prototype.compare = function (obj) {
        //判断传入值是否是一个对象
        if (typeof obj != "object" || obj === null) {
          return false;
        }

        //获取两个对象的key做比较 如果不同返回false
        let keyArr = Object.keys(this);
        let keyArr2 = Object.keys(obj);
        //比较两个数组长度 如果不同返回false
        if (keyArr.length !== keyArr2.length) {
          return false;
        }
        //循环对比每一个key
        for (let i = 0; i < keyArr.length; i++) {
          if (keyArr[i] !== keyArr2[i]) {
            return false;
          }
        }

        //对比value值
        let arr1 = [];
        let arr2 = [];
        for (const [key, value] of Object.entries(this)) {
          arr1.push(value);
        }
        for (const [key, value] of Object.entries(obj)) {
          arr2.push(value);
        }
        // 此时两个数组长度肯定一致 因为经过上面的key的对比
        for (let index = 0; index < arr1.length; index++) {
          if (arr1[index] instanceof Object && arr2[index] instanceof Object) {
            //有嵌套结构 递归调用
            if (!arr1[index].compare(arr2[index])) {
              return false;
            }
          } else {
            if (arr1[index] !== arr2[index]) {
              return false;
            }
          }
        }

        return true;
      };

      let obj2 = {
        a: 1,
        b: 2,
        c: {
          d: {
            e: function () {
              console.log(123);
            },
          },
        },
      };
      let obj = {
        a: 1,
        b: 2,
        c: {
          d: {
            e: function () {
              console.log(123);
            },
          },
        },
      };
      console.log(obj.compare(obj2));
    </script>
  </body>
</html>
